package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;
//model layer package has to be imported
/**
 * Controller class for the DVD collection
 * 
 * @author (DM80 Group 5, Anders, Michael, Jesper, Lars og Kenni) 
 * @version (16/11-2012)
 */
public class DVDCtr
{
    //declare instance variables to reference containers
    //passed as arguments from the main class

    private DVDLibrary dvdLib;
    
    /**
     * Constructor for objects of class DVDCtr
     */
    public DVDCtr()//DVD container is to be passed as parameter from the main controller
    {
        dvdLib = DVDLibrary.getDvdLibrary();
    }
    
    //Dummy implementation: after implementing the model layer 
    //objects of class DVD are to be returned, not String
    public String findDVD(int _id)
    {
        DVD tempDVD = dvdLib.searchById(_id);
        
        String retStr = "";
        
        if(tempDVD != null)
        { 
            retStr = " ----------------------------------" + "\n";
            retStr += " DVD Info:" + "\n";
            retStr += " The ID for this cd: " + _id + "\n";
            retStr += " The Title is: " + tempDVD.getTitle() + "\n";
            retStr += " The Artist is: " + tempDVD.getArtist() + "\n";
            retStr += " The release Date is: " + tempDVD.getReleaseDate() + "\n";
            retStr += " The description is: " +  tempDVD.getDescription();
            
            ArrayList<Sample> _dvdSamples = tempDVD.getAllSamples();
            
            if(_dvdSamples != null)
            {
                retStr += "\n";
                retStr += " Dvd samples:" + "\n";
                for(Sample sample : _dvdSamples)
                {
                    retStr += " Serial nr: " + sample.getSerialNr() + " Aquire price: " + sample.getAcquirePrice() + "\n";    
                }
            }

            retStr += " ----------------------------------";
        }
        
        return retStr;
    }
    
    //Dummy implementation: more parameters are probaly to added
    public void createDVD(int _id, String _title, String _artist, String _releaseDate, String _description)
    {       
        DVD newDVD = new DVD(_id, _title, _artist, _releaseDate, _description);
        
        dvdLib.addDVD(newDVD);
    }
    
    public boolean deleteDVD(int _id)
    {
        boolean success = false;
        
        DVD tempDVD = dvdLib.searchById(_id);
       
        if(tempDVD != null)
        {
            dvdLib.deleteDVD(tempDVD);   
            
            success = true;
        }    
       
        return success;
    }
    
    public boolean createSample(int _dvdId, int _sampleSerialNr, String _sampleAcquireDate, double _sampleAcquirePrice)
    {
        boolean success = false;
        
        DVD dvd = dvdLib.searchById(_dvdId);
        
        if(dvd != null)
        {
            dvdLib.deleteDVD(dvd);
            
            Sample newSample = new Sample(_sampleSerialNr, _sampleAcquireDate, _sampleAcquirePrice);
            
            dvd.addSample(newSample);
            
            dvdLib.addDVD(dvd);
            
            success = true;
        }
        
        return success;
    }
    
    public String printAllDvds()
    {
        String retStr = "";
        
        ArrayList<DVD> allDvds = dvdLib.getAllDVDs();
        
        if(allDvds != null)
        {
            retStr += "\n";
            retStr += " All Dvds:" + "\n";
            
            for(DVD dvd : allDvds)
            {
                retStr += " id: " + dvd.getId() + " title: " 
                + dvd.getTitle() + " artist: " + dvd.getArtist() + "\n";
            }
        }
        
        return retStr;
    }
    
    public ArrayList<Sample> getSamplesByDvdId(int _id)
    {        
        DVD dvd = dvdLib.searchById(_id);
        
        return dvd.getAllSamples();
    }
    
    public ArrayList<DVD> getAllDvds()
    {
        return dvdLib.getAllDVDs();
    }
    
    public DVD getDvdById(int _id)
    {
        return dvdLib.searchById(_id);
    }
    
    public Sample getSampleFromDvdById(int _dvdId, int _serialNr)
    {
        DVD dvd = dvdLib.searchById(_dvdId);
        ArrayList<Sample> lstSamples = dvd.getAllSamples();
        
        boolean found = false;
        int index = 0;
        Sample retSample = null;
        
        while(!found && index < lstSamples.size())
        {
            Sample sample = lstSamples.get(index);
            
            if(sample.getSerialNr() == _serialNr)
            {
                retSample = sample;
                found = true;
            }
            else
            {
                index++;
            }
        }
        
        return retSample;
    }
}
